<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    width="850px"
  >
    <div class="form-box">
      <el-form
        ref="form"
        :model="form"
        :inline="true"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="游戏" prop="gameSn">
          <el-select v-model="form.gameSn" placeholder="请选择游戏" @change="gameChange">
            <el-option
              v-for="game in allGameList"
              :key="game.id"
              :label="game.name"
              :value="game.id"
            />
          </el-select>
        </el-form-item>
      
        <el-form-item label="游戏账号" prop="account">
          <el-input v-model="form.account" placeholder="请输入游戏账号" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
      
        <el-form-item label="备注" prop="remark">
          <el-input
              v-model="form.remark"
              type="textarea"
              placeholder="备注"
              :rows="3"
              style="width:600px"
            />
        </el-form-item>
     
       

        <div class="center">
          <el-button @click="close()">取消</el-button>
          <el-button type="primary" @click="submitForm('form')" v-loading="loading">提交</el-button>
        </div>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import { addBlack } from "../../../../api/black/black";
export default {
  name: "",
  data() {
    return {
      dialogVisible: false,
      loading: false,
    
      title: "新增黑名单",
      isEdit: false,
     
      // 表单参数
      form: {
        account: '',
        gameSn:'',
        gameName:'',
        mobile:'',
        remark:''
      },
      rules: {
        // gameSn: [
        //   { required: true, message: "请选择游戏", trigger: "change" }
        // ],
        account: [
          { required: true, message: "请输入账号", trigger: "blur" }
        ],
      }
    };
  },
  computed: {
    allGameList() {
      return this.$store.getters.allGameList;
    },
  },
  created() {
  },
  methods: {
    // 打开弹窗
    open() {
      // this.getTypeList();
      // this.newImages = [];
      // this.imgShowList = [];
      // this.imgFileList = [];
      // if (flag == "edit") {
      //   // this.title = "修改账单";
      //   // this.isEdit = true;
      //   // this.handleUpdate(data);
      // } else {
      //   this.isEdit = false;
      // }
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.reset();
      });
    },

    // 重置
    reset() {
      this.form = {
        account: '',
        gameSn:'',
        gameName:'',
        mobile:'',
        remark:''
      };
      this.$refs.form.resetFields();
    },
    gameChange(e){
      this.form.gameName = ''
      if(e){
        this.allGameList.forEach(r=>{
          if(r.id==e){
            this.form.gameName = r.name
          }
        })
      }
    },

  
 
    /** 查询详情进行回显 */
    handleUpdate(row) {
      const id = row.id || null;


    },
    submitForm(formName) {
      this.$refs["" + formName].validate(valid => {
        if (valid) {
          // // 拼接新上传的图片
          // if (this.newImages && this.newImages.length) {
           
          //   let newArr = [];
          //   this.newImages.forEach(img => {
          //     newArr.push({
          //       name: img.name,
          //       url: img.response.data.filePath
          //     });
          //   });
          //   this.form.img = JSON.stringify(this.imgFileList.concat(newArr));
          // } else {
          //   this.form.img = JSON.stringify(this.imgFileList);
          // }
          if (this.isEdit) {
            // 编辑
            // jizhangbenApi.editTallyBook(this.form).then(res => {
            
            //   this.$message.success(res.msg);
            //   this.dialogVisible = false;
            //   this.$parent.search();
            // });

          } else {
            // 新增
            //console.log(this.form);
            addBlack(this.form).then(res => {
          
              this.$message.success(res.msg);
              this.dialogVisible = false;
              this.$parent.search();
            });
          }
        } else {
          this.$alert("请填写完整！");
        }
      });
    },
    // 详情图片上传成功
    handleUploadSuccess(res, file, fileList) {
      if (res.code == 200) {
        // 取得新上传的文件
        this.newImages = fileList.filter(t => {
          return t.url.indexOf("blob:") > -1;
        });
      }
    },
    // 详情图片删除
    handleRemove(response, fileList, file) {
      const that = this;
      // 1.获取将要删除图片的临时路径
      const filePath = response.url;
      // 2.找到图片对应的索引值
      this.imgFileList.forEach(function(element, key) {
        if (element.url === filePath) {
          that.imgFileList.splice(key, 1); //回显的图片
        }
      });
      this.newImages.forEach(function(element, key) {
        if (element.url === filePath) {
          that.newImages.splice(key, 1); //新上传的图片
        }
      });
    },
    // 图片预览
    handlePictureCardPreview(file) {
      this.$viewerApi({
        images: [file.url]
      });
    },
    uploadNumber(file, fileList) {
      //限制图片数量
      this.$modal.msgError("最多只能上传50张图片");
    },
    close() {
      this.dialogVisible = false;
    }
  }
};
</script>
<style scoped lang="css">
.center {
  text-align: center;
}

/deep/ .el-upload-dragger{
  width: 150px;
  height: 150px;
}
</style>
